package org.wattdepot.cli;

import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Executes the fueltypes command.
 * 
 * @author Scott Wong and Kelli Sawai
 * 
 */
public class FuelType extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Executes and returns the results of the fueltypes command.
   * 
   * @param args The arguments required to execute this method.
   * @return The results of the command.
   */
  public String doCommand(String args) {
    try {
      if (args.matches("fueltypes\\s\\w*")) {
        String[] src = args.split("\\s");
        Source source = client.getSource(src[1]);

        String subPlant = getRelatedPlants(src[1]);

        if (subPlant.compareToIgnoreCase("none") == 0) {
          return source.getProperty("fuelType");
        }
        else {
          String[] holder = subPlant.split(", ");
          int lsfoCount = 0;
          int coalCount = 0;
          int wasteCount = 0;

          // initiated to 9 since no source has more than 8 subsources.
          String[] LSFO = new String[9];
          String[] coal = new String[9];
          String[] waste = new String[9];

          // adds up all the power from the subsources.
          for (int i = 0; i < holder.length; i++) {
            source = client.getSource(holder[i]);

            // if the fuel type is LSFO then save the source name.
            if (source.getProperty("fuelType").compareToIgnoreCase("LSFO") == 0) {
              LSFO[lsfoCount] = holder[i];
              lsfoCount++;
            }
            // if the fuel type is coal then save the source name.
            else if (source.getProperty("fuelType").compareToIgnoreCase("coal") == 0) {
              coal[coalCount] = holder[i];
              coalCount++;
            }
            // if the fuel type is waste then save the source name.
            else {
              waste[wasteCount] = holder[i];
              wasteCount++;
            }
          }

          StringBuffer buf = new StringBuffer();

          for (int i = 0; i < LSFO.length; i++) {
            // if there are no sources with LSFO type.
            if (LSFO[0] == null) {
              break;
            }
            else if (i == 0) {
              buf.append("LSFO: ").append(LSFO[i]);
            }
            // if there are no more LSFO sources.
            else if (LSFO[i] == null) {
              break;
            }
            else {
              buf.append(", ").append(LSFO[i]);
            }
          }

          for (int i = 0; i < waste.length; i++) {
            // if there are no sources with waste type.
            if (waste[0] == null) {
              break;
            }
            else if (i == 0) {
              buf.append("\nwaste: ").append(waste[i]);
            }
            // if there are no more waste sources.
            else if (waste[i] == null) {
              break;
            }
            else {
              buf.append(", ").append(waste[i]);
            }
          }

          for (int i = 0; i < coal.length; i++) {
            // if there are no sources with coal type.
            if (coal[0] == null) {
              break;
            }
            else if (i == 0) {
              buf.append("\ncoal: ").append(coal[i]);
            }
            // if there are no more coal sources.
            else if (coal[i] == null) {
              break;
            }
            else {
              buf.append(", ").append(coal[i]);
            }
          }
          return buf.toString();
        }
      }
      else {
        return "The command you have entered is invalid.";
      }
    }
    catch (ResourceNotFoundException e) {
      return resourceMessage;
    }
    catch (WattDepotClientException e) {
      return "An error occured while executing the requested command.";
    }
    catch (Exception e) {
      return "An error occured while executing the requested command." + e;
    }
  }

  /**
   * Determines if the source plant is associated with any other plants.
   * 
   * @param src The main plant.
   * @return The plants associated with the source plant.
   * @throws NotAuthorizedException If the user is not authorized to access the data.
   * @throws ResourceNotFoundException If the date requested isn't found.
   * @throws BadXmlException If there is a bad xml file.
   * @throws MiscClientException Covers all other client exceptions.
   */
  private String getRelatedPlants(String src) throws NotAuthorizedException,
      ResourceNotFoundException, BadXmlException, MiscClientException {
    /** Default for variable. */
    try {
      String none = "none";
      Source source = client.getSource(src);

      // Sources subSources
      String subSources = none;

      // saves all of the source's subsources to a string.
      if (!(source.getSubSources() == null)) {
        subSources = source.getSubSources().toString();
        subSources = subSources.replace("[", "");
        subSources =
            subSources.replace("http://server.wattdepot.org:8182/wattdepot/sources/", "");
        subSources = subSources.replace("]", "");
      }

      return subSources;
    }
    catch (ResourceNotFoundException e) {
      return resourceMessage;
    }
  }

}
